﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using ARR.Entities;
using ARR.UI.ViewModels;
using System.Collections.ObjectModel;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for StoresImporterView.xaml
    /// </summary>
    public partial class StoresImporterView : UserControl
    {
        public StoresImporterView()
        {
            InitializeComponent();
        }
 
        private void ResultsGV_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            StoresImporterViewModel vm = this.DataContext as StoresImporterViewModel;

            if (vm != null)
            {
                ImporterResult result = ResultsGV.SelectedItem as ImporterResult;

                if (result != null)
                {
                    List<TempStore> selectedStores = new List<TempStore>();

                    if (result.ItemId != 0)
                    {
                        selectedStores = vm.model.TempStoresCache.Where(x => x.Id == result.ItemId).ToList();
                    }
                    else if (result.Workaround == Workaround.Duplicated)
                    {
                        selectedStores = vm.model.TempStoresCache.Where(x => x.CountryId.ToString() == result.Param2 && x.Code == result.Param1).ToList();

                    }

                    if (selectedStores != null)
                    {
                        vm.model.TempStores = new ObservableCollection<TempStore>(selectedStores);

                        vm.TriggerPropertyChanged();
                    }

                }



                int tempId = result.ItemId;

                if (tempId > 0)
                {
                    TempStore store = vm.TempStores.SingleOrDefault(art => art.Id == tempId);

                    TempStoresGV.ScrollIntoViewAsync(store, //the row
                                         null, //the column
                                         new Action<FrameworkElement>((f) =>
                                         {
                                             (f as GridViewRow).IsSelected = true; // the callback method; if it is not necessary, you may set that parameter to null;
                                         }));
                }
            }
        }
    }
}
